package com.alibaba.springboot.health.controller;

import com.alibaba.springboot.health.pojo.Drug;
import com.alibaba.springboot.health.service.DrugService;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * (Drug)表控制层
 *
 * @author makejava
 * @since 2019-12-24 14:33:36
 */
@Controller
public class DrugController {

    private static final Logger LOGGER= LogManager.getLogger(DrugController.class);
    /**
     * 服务对象
     */
    @Autowired
    private DrugService drugService;

    /**
     * 通过主键查询单条数据
     *  xl
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/drug/project",method = RequestMethod.GET)
    public String searchOne(int id, Model model, HttpSession session) {
        Drug drug = drugService.queryById(id);
        LOGGER.info(drug);
        model.addAttribute("drug", drug);
        //session.setAttribute("drug", drug);
        return "project";
    }

    /**
     * 通过主键查询单条数据
     *zdj
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/drug/searchOne")
    public String  searchOne(Integer id,Model model) {
        Drug drug = drugService.queryById(id);
        model.addAttribute("drug",drug);
        return "drug-edit";
    }

    /**
     * xl
     * @param drugId
     * @param model
     * @return
     */
    @RequestMapping("/drug/show/{drugId}")
    public String showOne(@PathVariable("drugId") int drugId, Model model){
        LOGGER.info("11111111111111111111===="+drugId);
        Drug drug = drugService.queryById(drugId);
        LOGGER.info(drug+"====222222222222222222222");
        model.addAttribute("drugInfo", drug);
        return "order";
    }
    /**
     * all
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param drug 实体
     * @return PageInfo<Drug> 分页数据集合
     */
    @RequestMapping(value = "/drug/show",method = RequestMethod.POST)
    @ResponseBody()
    public PageInfo<Drug> show(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "pageSize",defaultValue = "1")int pageSize, Drug drug,@RequestParam(value = "fuzzy",required = false) String fuzzy){
        if (drug.getDrugTypeId()!=null){
            return drugService.queryAll(page, pageSize, drug);
        }
        if(fuzzy!=null){
            return drugService.queryByFuzzy(page, pageSize, fuzzy);
        }
        return drugService.queryAll(page, pageSize, drug);
    }

    /**
     * zdj
     * 权限  后台用户  drugadmin
     * @param page
     * @param pageSize
     * @param drug
     * @param model
     * @return
     */
   @RequiresPermissions(value = {"/drug/searchAll"})
    @RequestMapping("/drug/searchAll")
    public String searchAll(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "pageSize",defaultValue = "5")int pageSize, Drug drug, Model model){
        PageInfo<Drug> drugs = drugService.queryAll(page, pageSize, drug);
        model.addAttribute("drugs",drugs);
        return "member-drug";
    }

    /**
     * zdj
     * @param drug
     * @return
     */
    @RequestMapping("/drug/update")
    public Drug update(Drug drug){
        Drug update = drugService.update(drug);
        return update;
    }
}